package yja.gzw.app.activity

import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Environment
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Toast
import cn.jzvd.Jzvd
import yja.gzw.app.R
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityShipingbianjibofangBinding
import yja.gzw.app.viewmodel.Mainviewmodel
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.net.URL
import java.net.URLConnection
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

class ShipingbianjibofangActivity :BaseActivity<Mainviewmodel, ActivityShipingbianjibofangBinding>(R.layout.activity_shipingbianjibofang){
    override fun initView() {

        val lianjie=intent.getStringExtra("lianjie")


        binding.videoplayer.setUp(lianjie
            , "播放视频", Jzvd.SCREEN_WINDOW_NORMAL);
        binding.videoplayer.thumbImageView.setImageURI(Uri.parse("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"));

        binding.quedingwancheng.setOnClickListener {

            if (lianjie!!.isNotEmpty()) {
                downloadVideo(lianjie)
                Toast.makeText(this, "视频下载中请稍后。。。", Toast.LENGTH_SHORT).show()

            } else {
                Toast.makeText(this, "视频链接为空", Toast.LENGTH_SHORT).show()
            }
        }



    }
    private fun downloadVideo(videoUrl: String) {
        Thread {
            try {
                val url = URL(videoUrl)
                val connection: URLConnection = url.openConnection()
                connection.connect()

                val input: InputStream = connection.getInputStream()

                val currentDateTime = LocalDateTime.now()
                val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss")
                val formattedDateTime = currentDateTime.format(formatter)


                val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), "${formattedDateTime}downloaded_video.mp4")
                Log.e("视频地址", file.path)
                val output = FileOutputStream(file)

                val data = ByteArray(4096)
                var count: Int
                while (input.read(data).also { count = it } != -1) {
                    output.write(data, 0, count)
                }

                output.flush()
                output.close()
                input.close()

                // 通知系统媒体库更新
                MediaScannerConnection.scanFile(this, arrayOf(file.absolutePath), null) { path, uri ->
                    Handler(Looper.getMainLooper()).post {
                        finish()
                        Log.e("视频保存路径",file.absolutePath)
                        Toast.makeText(this, "下载完成", Toast.LENGTH_SHORT).show()
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Handler(Looper.getMainLooper()).post {
                    Toast.makeText(this, "下载失败", Toast.LENGTH_SHORT).show()
                }
            }
        }.start()
    }

    override fun dataObservese() {

    }
}